package com.iotechn.unimall.app.api.running;

import cn.hutool.core.collection.CollectionUtil;
import com.iotechn.unimall.core.exception.AppServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.RunningGroupDO;
import com.iotechn.unimall.data.domain.RunningGroupUserDO;
import com.iotechn.unimall.data.domain.UserDO;
import com.iotechn.unimall.data.enums.UserLevelType;
import com.iotechn.unimall.data.mapper.RunningGroupMapper;
import com.iotechn.unimall.data.mapper.RunningGroupUserMapper;
import com.iotechn.unimall.data.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by rize on 2019/6/30.
 */
@Service
public class RunningGroupServiceImpl implements RunningGroupService {

    @Autowired
    private RunningGroupMapper runningGroupMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RunningGroupUserMapper runningGroupUserMapper;


    @Override
    public RunningGroupDO getRunningGroupInfo(Long userId) {

        UserDO userDO = userMapper.selectById(userId);
        if(userDO==null){
            return null;
        }
        RunningGroupDO runningGroupDO = runningGroupMapper.selectById(userDO.getRunningGroupId());

        return runningGroupDO;
    }

    @Override
    public Boolean joinGroup(String inviteCode, Long userId) throws ServiceException{
        //根据邀请码查询对应的组，然后给更新用户所属的组
        RunningGroupDO runningGroupDO = runningGroupMapper.selectByInviteCode(inviteCode);
        if(runningGroupDO==null){
            throw new AppServiceException(ExceptionDefinition.INVITE_CODE_NO_HASED);
        }

        //判断当前用户拥有跑团的数量，目前不能操作10个
        List<RunningGroupUserDO> runningGroupUserDOS = runningGroupUserMapper.selectByUserId(userId);
        if(CollectionUtil.isNotEmpty(runningGroupUserDOS)&&runningGroupUserDOS.size()>=10){
            throw new AppServiceException(ExceptionDefinition.USER_MAX_GROUP_NUM);
        }


        //加入跑团的时候要看当前用户是否有跑团
        UserDO userDO = userMapper.selectById(userId);
        if(userDO!=null&&(userDO.getRunningGroupId()==null||userDO.getLevel()<1)){
            UserDO update=new UserDO();
            update.setId(userId);
            update.setLevel(UserLevelType.VIP.getCode());
            update.setRunningGroupId(runningGroupDO.getId());
            update.setGmtUpdate(new Date());
            userMapper.updateById(update);
        }

        RunningGroupUserDO runningGroupUserDO = runningGroupUserMapper.selectByGroupIdAndUserId(runningGroupDO.getId(), userId);
        if(runningGroupUserDO==null){
            runningGroupUserDO =new RunningGroupUserDO();
            runningGroupUserDO.setRunningGroupId(runningGroupDO.getId());
            runningGroupUserDO.setUserId(userId);
            runningGroupUserDO.setLevel(UserLevelType.VIP.getCode());
            runningGroupUserDO.setGmtCreate(new Date());
            runningGroupUserDO.setGmtUpdate(new Date());

            runningGroupUserMapper.insert(runningGroupUserDO);
        }
        return true;
    }

    @Override
    public Boolean updateGroupInfo(String groupName, String inviteCode, Long userId) throws ServiceException {
        //邀请码4-8位
        if(inviteCode.length()<4||inviteCode.length()>8){
            throw new AppServiceException(ExceptionDefinition.INVITE_CODE_LENTH);
        }
        //跑团名称最多12个字
        if(groupName.length()<3||groupName.length()>12){
            throw new AppServiceException(ExceptionDefinition.GROUP_NAME_LENTH);
        }

        Long runningGroupId = -1L;
        UserDO userDO = userMapper.selectById(userId);
        if(userDO!=null){
            runningGroupId = userDO.getRunningGroupId();
        }
        //判断邀请码是否重复
        Integer count1 = runningGroupMapper.selectCountByInviteCode(inviteCode,runningGroupId);
        if(count1>0){
            throw new AppServiceException(ExceptionDefinition.INVITE_CODE_HASED);
        }
        //判断组名称是否重复
        Integer count2 = runningGroupMapper.selectCountByGroupName(groupName,runningGroupId);
        if(count2>0){
            throw new AppServiceException(ExceptionDefinition.GROUP_NAME_HASED);
        }


        if(userDO!=null&&userDO.getRunningGroupId()!=null){
            RunningGroupDO runningGroupDO=new RunningGroupDO();
            runningGroupDO.setId(userDO.getRunningGroupId());
            runningGroupDO.setGroupName(groupName);
            runningGroupDO.setInviteCode(inviteCode);
            runningGroupDO.setGmtUpdate(new Date());

            runningGroupMapper.updateById(runningGroupDO);
        }
        throw new AppServiceException("修改成功", 200);
    }

    @Transactional
    @Override
    public Boolean addGroupInfo(String groupName, String inviteCode, Long userId) throws ServiceException {
        //邀请码4-8位
        if(inviteCode.length()<4||inviteCode.length()>8){
            throw new AppServiceException(ExceptionDefinition.INVITE_CODE_LENTH);
        }
        //跑团名称最多12个字
        if(groupName.length()<3||groupName.length()>12){
            throw new AppServiceException(ExceptionDefinition.GROUP_NAME_LENTH);
        }

        //判断邀请码是否重复
        Integer count1 = runningGroupMapper.selectCountByInviteCode(inviteCode,null);
        if(count1>0){
            throw new AppServiceException(ExceptionDefinition.INVITE_CODE_HASED);
        }
        //判断组名称是否重复
        Integer count2 = runningGroupMapper.selectCountByGroupName(groupName,null);
        if(count2>0){
            throw new AppServiceException(ExceptionDefinition.GROUP_NAME_HASED);
        }

        //判断当前用户拥有跑团的数量，目前不能操作10个
        List<RunningGroupUserDO> runningGroupUserDOS = runningGroupUserMapper.selectByUserId(userId);
        if(CollectionUtil.isNotEmpty(runningGroupUserDOS)&&runningGroupUserDOS.size()>=10){
            throw new AppServiceException(ExceptionDefinition.USER_MAX_GROUP_NUM);
        }

        RunningGroupDO runningGroupDO=new RunningGroupDO();
        runningGroupDO.setGroupName(groupName);
        runningGroupDO.setInviteCode(inviteCode);
        runningGroupDO.setGmtCreate(new Date());
        runningGroupDO.setGmtUpdate(new Date());

        runningGroupMapper.insert(runningGroupDO);

        Long runningGroupId = runningGroupDO.getId();

        //保存用户跑团的关联关系
        RunningGroupUserDO runningGroupUserDO = new RunningGroupUserDO();
        runningGroupUserDO.setRunningGroupId(runningGroupId);
        runningGroupUserDO.setUserId(userId);
        runningGroupUserDO.setLevel(UserLevelType.ADMIN.getCode());
        runningGroupUserDO.setGmtCreate(new Date());
        runningGroupUserDO.setGmtUpdate(new Date());
        runningGroupUserMapper.insert(runningGroupUserDO);

        throw new AppServiceException("创建成功", 200);
    }
}
